package com.cy.myblog.action;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;

@ParentPackage("basePackage")
@Namespace("/")
public class BaseAction extends ActionSupport implements  SessionAware, RequestAware, ApplicationAware{

	protected Map<String, Object> session;
	protected Map<String, Object> request;
	protected Map<String, Object> application;
	
	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	public void writeJson(Object obj){
		try {
			String json=JSON.toJSONStringWithDateFormat(obj,"yyyy-MM-dd HH:mm:ss");
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
			ServletActionContext.getResponse().getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	protected void clearSession(){
		SessionMap session = (SessionMap) ActionContext.getContext().getSession();
		HttpSession httpSession = ServletActionContext.getRequest().getSession();
		
		try {
			session.remove("loginUser");
			session.clear();
			httpSession.invalidate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void addCookie(HttpServletResponse res,String name,String value,int maxAge){
		String path = ServletActionContext.getRequest().getContextPath();
		Cookie cookie = new Cookie(name,value);
		cookie.setMaxAge(maxAge);
		cookie.setPath(path);
		res.addCookie(cookie);
	}
	
	protected void removeCookie(HttpServletResponse res,HttpServletRequest req,String name){
		String path = ServletActionContext.getRequest().getContextPath();
		Cookie[] coks = req.getCookies();
		if(coks != null && coks.length > 1) {
			for (Cookie cok : coks) {
				if (cok.getName().equals(name)){
					cok.setValue("");
					cok.setMaxAge(-100);
					cok.setPath("/");
					res.addCookie(cok);
				}
			}
		}
	}

	protected String getIpAddr() {
		String ipAddress = null;
		//ipAddress = ServletActionContext.getRequest().getRemoteAddr();
		ipAddress = ServletActionContext.getRequest().getHeader("x-forwarded-for");
		if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = ServletActionContext.getRequest().getHeader("Proxy-Client-IP");
		}
		if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = ServletActionContext.getRequest().getHeader("WL-Proxy-Client-IP");
		}
		if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = ServletActionContext.getRequest().getRemoteAddr();
			if(ipAddress.equals("127.0.0.1")){
				//根据网卡取本机配置的IP
				InetAddress inet=null;
				try {
					inet = InetAddress.getLocalHost();
				} catch (UnknownHostException e) {
					e.printStackTrace();
				}
				ipAddress= inet.getHostAddress();
			}

		}

		//对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
		if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15
			if(ipAddress.indexOf(",")>0){
				ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));
			}
		}
		return ipAddress;
	}


}
